import parsel
import  requests
import  re
import json
from bs4 import BeautifulSoup

html_str="""
<!DOCTYPE html>
<html lang="zh-cn">
<head>
     <meta charset="utf-8">
     <title>Document</title>
</head>
<body>
{article}
</body>
</html>
"""
# #智联招聘
url='https://xiaoyuan.zhaopin.com/job/CC235435310J40670560311'
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
'Referer' :  'https://xiaoyuan.zhaopin.com/',
'Cookie' :'_uab_collina=175334881553871327218272; x-zp-client-id=e5eeb0c1-10a9-45fb-b3ec-6e7e5ae51ffe; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkchannel=%7B%22prop%22%3A%7B%22_sa_channel_landing_url%22%3A%22https%3A%2F%2Flanding.zhaopin.com%2Fregister%3Fidentity%3Dc%26channel_name%3Dbaidu_sem_track%26callback_id%3DIQJ0tYqm%26_data_version%3D0.6.1%26channel_utm_content%3Dpp%26project%3Dzlclient%26channel_utm_medium%3Dcpt%26tid%3DxTB%26channel_link_type%3Dweb%26channel_utm_source%3Dbaidupcpz_x%26hash_key%3DuaKIYNjV6PvgIWUUAJ9n%26sat_cf%3D2%26channel_utm_campaign%3Dzbt%26channel_utm_term%3D01%26_channel_track_key%3DM9kzlBZB%26link_version%3D1%26channel_keyword_id%3D%7Bkeywordid%7D%26channel_ad_id%3D%7Bcreative%7D%26channel_account_id%3D%7Buserid%7D%26channel_keyword%3D%7Bkw_enc_utf8%7D%26channel_adgroup_id%3D%7Bunitid%7D%26channel_campaign_id%3D%7Bplanid%7D%22%7D%7D; zp_passport_deepknow_sessionId=0f76373esc5e06488fb188ed5cf1a9c52646; at=7ab6a5736a5249219526253a86e696a6; rt=8bd1ebcd2e694ac4b3bccc53e11cf139; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221059369081%22%2C%22first_id%22%3A%221983bbb3f40312-0a79ecc1900c5c8-26011151-2621440-1983bbb3f41531%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E4%BB%98%E8%B4%B9%E5%B9%BF%E5%91%8A%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%99%BA%E8%81%94%E6%8B%9B%E8%81%98%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Fother.php%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTk4M2JiYjNmNDAzMTItMGE3OWVjYzE5MDBjNWM4LTI2MDExMTUxLTI2MjE0NDAtMTk4M2JiYjNmNDE1MzEiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIxMDU5MzY5MDgxIn0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%221059369081%22%7D%2C%22%24device_id%22%3A%221983bbb3f40312-0a79ecc1900c5c8-26011151-2621440-1983bbb3f41531%22%7D; locationInfo_job={%22code%22:%22635%22%2C%22name%22:%22%E5%8D%97%E4%BA%AC%22}'}
response = requests.get(url,headers=headers)



selector=parsel.Selector(response.text)
# css 选择器
content_1=selector.css('.job-banner').get()
content_2=selector.css('.main-body__block').get()
content_3 = (content_1 if content_1 is not None else "") + (content_2 if content_2 is not None else "")

print(response.text)
# 你的HTML内容
html_content = response.text

# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_content, 'html.parser')
script_tag = soup.find('script', string=re.compile('window.__INITIAL_DATA__'))









# 提取职位的基本信息
job_info = {
    'position_name': soup.find('h1',class_='job-banner__name').text.strip(),  # 获取职位名称
    'ZhangYe': soup.find('div', class_="job-info__tags job-info__major-tags").text.strip(),  #专业
    'city': soup.find('span', class_="job-banner__desc-text").text.strip(),  # 城市
}

# 转换为JSON格式
job_info_json = json.dumps(job_info, ensure_ascii=False, indent=2)
# 打印或保存JSON格式的数据
print(job_info_json)




html = html_str.format(article=content_3)
htmlPath='D:/project/其他/HTML\\'+'1.html'
htmlPathPDF='D:/project/其他/HTML\\'+'1.pdf'
with open(htmlPath,mode='w',encoding='utf-8') as f:
     f.write(html)

options = {
    'no-images': '',  # 禁用图片加载
}
import pdfkit
config=pdfkit.configuration(wkhtmltopdf=r'D:/Work/pdf/wkhtmltopdf/bin/wkhtmltopdf.exe')
pdfkit.from_file(htmlPath,htmlPathPDF,configuration=config,options=options)







# #boss
# # url='https://www.zhipin.com/job_detail/d63ac357e6e8302d03Fz3di-GVdZ.html'
# # headers = {
# # 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
# # 'Referer' :  'https://www.zhipin.com/nanjing/',
# # 'Cookie' :'lastCity=101190100; ab_guid=a593ce5f-1ac2-4f2e-9753-7fe686a7d0d6; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1753339964,1753349121; HMACCOUNT=3CF282FDADD6F5A1; __g=sem_pz_bdpc_dasou_title; __l=l=%2Fwww.zhipin.com%2Fsem%2F10.html%3Fsid%3Dsem_pz_bdpc_dasou_title&r=https%3A%2F%2Fwww.baidu.com%2Fother.php%3Furl%3DK00000jMSAJ2UJz6YvHpUXQrSUo2sVNVBqkKri3v4uCLrAjwZ75RedZ2kUTtutJBJjZxH8Id3OGY8lLI2ztJa6QbqdYM_elZDI1su_bNbCfmyZ-minwnGIGh4bc3C77RMkzaFdLi2lx3-44bblW6y0fvp7Q4hKNPbOy4n5znq1nBjAdcbCpOwNXmH8M1A2yxibxlk9Rat0vQr9jENjQYPb8x_94S.Db_NR2Ar5Od663rj6t8AGSPticcYlm2erxX5DkAMHz3SZ-E8kstV1nU_ZQfxfCGRojPakEvTXb0.TLFWgv-b5HDkrfK1ThPGujYknHb0THdaHdPH0ZwV5RF5Rdn0IgP-T-qYXgK-5H00mywxIZ-suHY10ZIEThfqmhq1TsKzmLmqn0KdThkxpyfqnHRvrHDznjb1PfKVINqGujYkPWTsnW04PsKVgv-b5HDYnj6kPjnd0AdYTAkxpyfqnHc3nWm0TZuxpyfqn0KGuAnqHbG2RsKspyfqnHb0mv-b5HRd0APzm1YYPj0krf%26ck%3D3557.5.156.336.151.460.150.67%26dt%3D1753349116%26wd%3DBOSS%26tpl%3Dtpl_12826_36977_0%26l%3D1569120935%26ai%3D0_60872259_1_1%26us%3DlinkVersion%253D1%2526compPath%253D10036.0-10032.0%2526label%253D%2525E4%2525B8%2525BB%2525E6%2525A0%252587%2525E9%2525A2%252598%2526linkType%253D%2526linkText%253DBOSS%2525E7%25259B%2525B4%2525E8%252581%252598%2525E2%252580%252594%2525E2%252580%252594%2525E6%252589%2525BE%2525E5%2525B7%2525A5%2525E4%2525BD%25259C%2525EF%2525BC%25258C%2525E4%2525B8%25258ABOSS%2525E7%25259B%2525B4%2525E8%252581%252598%2525EF%2525BC%25258C&g=%2Fwww.zhipin.com%2Fsem%2F10.html%3Fsid%3Dsem_pz_bdpc_dasou_title&s=3&friend_source=0&s=3&friend_source=0; wt2=DKQ-uVqTyFTsa8m5FhJozI4Kd3A5j0QFGxNcYYDelVK8gqHsHiaZl9kaxQSp_S_e5WjLWHhPsFgdqwSqCzN6mfA~~; wbg=0; zp_at=-xTx3DLYwazu9cZMKQVfNrcikpyG0UM1xqJNB6RiQRM~; bst=V2RtIuE-z53lhgXdJsyxweKCmw7DrRwA~~|RtIuE-z53lhgXdJsyxweKCmw7D3exg~~; __c=1753349121; __a=29939811.1753339958.1753339958.1753349121.25.2.14.14; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1753349339; __zp_stoken__=c819fw4XCt8OMCjdaDgZxCcKwWsK8WUrCu8K6wrnCscKgSVp7WltBWMK6QVtcwrB2wrjCqsKdQ8KSRMOzwr5OwrzCkl3CjFPDuUvCk8K7wovCqsKTxJXDo8KLxZXEoE7CscKaOC0DAQAPAgUHBgkEcHIOAQwHBQQLBgQGBwgFNi3DsMKUw5c0Nzs%2FIEpISQ1MW15AUEABVEFMMjcJWw8INyswPzIzwrkwwrQywrsxwrs%2FwrozwrowPzozMcK2woInIMK2awvCtcOIDUYLCwfCscOjC8OCW8K2w4zDocK3LS02OcKyxLI6Nx04NDY5ODU7NjctNWvDgVjCuMOOw63CtBwoMhQ%2BNzg8NjY3OD40OCM4OgEgNz8iOAgGCwkGJzrCu8KYwrPDnDc4'}
# # response =requests.get(url,headers=headers)
# # print(response.text)
#
